﻿var uniqueness;

$(function () {
    $('#unLogin').css({ "visibility": "hidden" });
    $('#unEmail').css({ "visibility": "hidden" });

    uniqueness = Uniqueness();
    uniqueness.initialize();
});

function Uniqueness() {

    this.LOGIN = "#tbLogin";
    this.EMAIL = "#tbEmail";

    var _instance = new Object();
    var _this = this;

    this.loginKeyUp = function (event) {

        var login = $(_this.LOGIN).val();

        $.ajax(
        {
            type: "POST",
            url: "/Registration/UnLogin",
            data: { value: login },
            success: function (result) {
                if (result == 'true') {
                    $('#unLogin').css({ "visibility": "visible" });
                    $("#nextStep").attr("disabled", true);
                }
                else {
                    $('#unLogin').css({ "visibility": "hidden" });
                    $("#nextStep").removeAttr("disabled");
                }
            }
        }
        );
    };


    this.emailKeyUp = function (event) {

        var email = $(_this.EMAIL).val();

        $.ajax(
        {
            type: "POST",
            url: "/Registration/UnEmail",
            data: { value: email },
            success: function (result) {
                if (result == 'true') {
                    $('#unEmail').css({ "visibility": "visible" });
                    $("#nextStep").attr("disabled", true);
                }
                else {
                    $('#unEmail').css({ "visibility": "hidden" });
                    $("#nextStep").removeAttr("disabled");
                }
            }
        }
        );
    };

    _instance.initialize = function () {
        $(_this.LOGIN).keyup(_this.loginKeyUp);
        $(_this.LOGIN).click(_this.loginKeyUp);

        $(_this.LOGIN).click(_this.emailKeyUp);
        $(_this.EMAIL).keyup(_this.emailKeyUp);
    };

    return _instance;
};
